/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CSharpKit.Convertions
{
    public sealed class Base64Converter
    {
        Base64Converter() { }
        public static readonly Base64Converter Instance = new Base64Converter();

        public string ImageToBase64(Image image)
        {
            var result = default(String);

            try
            {
                var bytes = ImageToBytes(image);
                string base64 = Convert.ToBase64String(bytes);

                // MemoryStream ms = new MemoryStream();
                // bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                // byte[] bytes = ms.ToArray();
                // ms.Close();
                // string result = "data:image/png;base64," + Convert.ToBase64String(bytes);

                result = base64;
            }
            catch (System.Exception)
            {
                throw;
            }

            return result;
        }

        static byte[] ImageToBytes(Image image)
        {
            ImageFormat format = image.RawFormat;
            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }

                // ms.to

                byte[] buffer = new byte[ms.Length];
                //Image.Save()会改变MemoryStream的Position，需要重新Seek到Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);

                return buffer;
            }
        }



        // private void ToBase64(object sender, EventArgs e)

        // {

        //     Image img = this.pictureBox.Image;

        //     BinaryFormatter binFormatter = new BinaryFormatter();

        //     MemoryStream memStream = new MemoryStream();

        //     binFormatter.Serialize(memStream, img);

        //     byte[] bytes = memStream.GetBuffer();

        //     string base64 = Convert.ToBase64String(bytes);

        //     this.richTextBox.Text = base64;

        // }

        // }}@@@
    }

}
